package com.yanqu.road.server.cmd.activity.pawnshop;

import com.yanqu.road.entity.activity.pawnshop.UserPawnshopItem;
import com.yanqu.road.server.pb.PawnshopPb;
import com.yanqu.road.pb.activity.PawnshopProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.pawnshop.PawnshopModule;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_PAWNSHOP_ITEM_REPAIR, desc = "请求物件修复")
public class PawnshopItemRepairCmd extends AbstractCommand {

	private long itemId;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		PawnshopProto.PawnshopItemRepairReqMsg reqMsg = PawnshopProto.PawnshopItemRepairReqMsg.parseFrom(packet.getBytes());
		itemId = reqMsg.getItemId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		ret = player.getModule(PawnshopModule.class).repairItem(itemId);
	}

	@Override
	public void sendData(GamePlayer player) {
		PawnshopProto.PawnshopItemRepairRespMsg.Builder respMsg = PawnshopProto.PawnshopItemRepairRespMsg.newBuilder();
		respMsg.setRet(ret);
		if(0 == ret){
			UserPawnshopItem userItem = player.getModule(PawnshopModule.class).getUserPawnshopItem(itemId);
			PawnshopProto.DPawnshopItem.Builder itemMsg = PawnshopPb.parseDPawnshopItem(userItem);
			if(null != itemMsg){
				respMsg.setItemData(itemMsg);
			}
		}
		player.sendPacket(Protocol.U_PAWNSHOP_ITEM_REPAIR, respMsg);
	}

}
